﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using Caifu.Clients;

namespace Caifu
{
	/// <summary>
	/// 
	/// </summary>
	public static class FundUtil
	{
		/// <summary>
		/// 获取基金信息。
		/// 首先从当前进程缓存查询，如果不存在则从数据源查询，并缓存至当前进程，有效时间5分钟。
		/// </summary>
		/// <param name="fundId">基金编号</param>
		/// <returns></returns>
		public static Fund GetFund(Guid fundId)
		{
			var key = "Caifu.Fund.FundId." + fundId;
			var fund = HttpRuntime.Cache.Get(key);
			if (fund == null)
			{
				fund = FundClient.GetFund(fundId);
				if (fund != null)
				{
					HttpRuntime.Cache.Insert(key, fund, null,
						DateTime.Now.Add(new TimeSpan(0, 5, 0)),
						System.Web.Caching.Cache.NoSlidingExpiration);
				}
			}
			return (Fund)fund;
		}

		/// <summary>
		/// 获取基金信息。
		/// 首先从当前进程缓存查询，如果不存在则从数据源查询，并缓存至当前进程，有效时间5分钟。
		/// </summary>
		/// <param name="fundCode"></param>
		/// <returns></returns>
		public static Fund GetFund(string fundCode)
		{
			var key = "Caifu.Fund.FundCode." + fundCode;
			return CaifuUtil.GetValueFromCache(key, FundClient.GetFund, fundCode, new TimeSpan(0, 5, 0));
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		/// <returns></returns>
		public static string GetFundCode(Guid fundId)
		{
			var obj = GetFund(fundId);
			return obj == null ? "" : obj.Code;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		/// <returns></returns>
		public static string GetFundName(Guid fundId)
		{
			var obj = GetFund(fundId);
			return obj == null ? "" : obj.Name;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundCode"></param>
		/// <returns></returns>
		public static string GetFundName(string fundCode)
		{
			var obj = GetFund(fundCode);
			return obj == null ? "" : obj.Name;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		/// <returns></returns>
		public static string GetFundFullName(Guid fundId)
		{
			var obj = GetFund(fundId);
			return obj == null ? "" : obj.FullName;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundCode"></param>
		/// <returns></returns>
		public static string GetFundFullName(string fundCode)
		{
			var obj = GetFund(fundCode);
			return obj == null ? "" : obj.FullName;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundId"></param>
		/// <returns></returns>
		public static string GetFundAbbrName(Guid fundId)
		{
			var obj = GetFund(fundId);
			return obj == null ? "" : obj.AbbrName;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="fundCode"></param>
		/// <returns></returns>
		public static string GetFundAbbrName(string fundCode)
		{
			var obj = GetFund(fundCode);
			return obj == null ? "" : obj.AbbrName;
		}

		/// <summary>
		/// 从基金的前端代码、后端代码、证券代码匹配。
		/// </summary>
		/// <param name="code">不明确代码</param>
		/// <returns></returns>
		public static Fund GetFundByAnyCode(string code)
		{
			var fund = GetFund(code);
			if (fund != null) return fund;

			var funds = FundClient.GetAllFunds();
			var fundDict1 = funds.Where(p => !String.IsNullOrEmpty(p.FrontEndCode))
								.ToDictionary(p => p.FrontEndCode, p => p);
			var fundDict2 = funds.Where(p => !String.IsNullOrEmpty(p.BackEndCode))
								.ToDictionary(p => p.BackEndCode, p => p);

			if (fundDict1.TryGetValue(code, out fund))
			{
				return fund;
			}
			if (fundDict2.TryGetValue(code, out fund))
			{
				return fund;
			}

			return null;
		}
	}
}
